package cn.afactory.tygz.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.controller.SearchController;
import cn.afactory.tygz.model.CompanyInfo;
import cn.afactory.tygz.model.CompanyInfoList;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.ui.activities.TopicDetailActivity;
import cn.afactory.tygz.ui.adapter.ApartmentListAdapter;
import cn.afactory.tygz.ui.adapter.CompanyListAdapter;
import cn.afactory.tygz.ui.base.activities.SearchActivity;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshBase;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.ToastUtils;
import cn.afactory.tygz.ui.base.views.search.ISearch;
import cn.afactory.tygz.ui.base.views.search.SearchSourcePager;
import cn.afactory.tygz.ui.base.views.search.SearchType;

/**
 * Created by Kenneth on 15/9/29.
 */
public class SearchResultCompanyListFragment extends cn.afactory.tygz.ui.base.BaseFragment implements ISearch{

    private SearchController mController;
    protected CompanyListAdapter mAdapter;
    private List<CompanyInfo> mList = new ArrayList<CompanyInfo>();

    protected SearchSourcePager mSearchSourcePager;
    protected SearchType mSearchType;
    protected String mSearchContent;

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SearchController.MSG_SEARCH_COMPANY_OK:
                if (msg.obj instanceof CompanyInfoList) {
                    hideErrorPage(-1);
                    CompanyInfoList t = (CompanyInfoList) msg.obj;
                    if (t.value != null && t.value.size() >= 0) {
                        if(mAdapter.getCount() == 0 && t.value.size() == 0){
//                            showNullPage(true);
                            if (mActivity instanceof SearchActivity) {
                                ((SearchActivity) mActivity).onNoCompanySearched();
                            }
                            return ;
                        }
                        try {
                            mList.addAll(t.value);
                            mAdapter.setData(mList);
                            mPageIndex = (mList.size()/mPageSize) + 1;

                            if (plistview.isRefreshing()) {
                                plistview.onRefreshComplete();
                            }
                            if (t.value.size() < mPageSize) {
                                plistview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                            } else {
                                plistview.setMode(PullToRefreshBase.Mode.BOTH);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (mAdapter.getCount() == 0) {
                            ToastUtils.show(mContext,"没有搜索到相关内容");
                        }
                    }
                }
                break;
            case SearchController.MSG_SEAECH_COMPANY_KO:
                if (mAdapter.getCount() == 0) {
                    hideErrorPage(-1);
                    showErrorPage(msg.arg1, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            startLoadData(true);
                        }
                    });
                } else {
                    LocalUtils.showToast(mContext, (String) msg.obj);
                }
                break;
            default:
                break;
        }

        // 结束下拉刷新
        if (plistview.isRefreshing()) {
            plistview.onRefreshComplete();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addTitleBar = false;
        mController = new SearchController(mContext, mUIHandler);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fg_restaurant_list, null);
        initViews(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    /**
     * 初始化组件 views
     *
     * @param view
     */
    private void initViews(View view) {
        plistview = (PullToRefreshListView) view.findViewById(R.id.plv_subject);
        mAdapter = new CompanyListAdapter(mContext, mList);
        plistview.setAdapter(mAdapter);
        plistview.setMode(PullToRefreshBase.Mode.BOTH);
        plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(false);
            }
        });

        plistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView lv = plistview.getRefreshableView();
                if (position >= lv.getHeaderViewsCount()) {
                    position -= lv.getHeaderViewsCount();
                }

                CompanyInfo companyInfo = (CompanyInfo) mAdapter.getItem(position);
                NaviUtils.gotoUserList(mContext, -1, companyInfo.companyName, companyInfo.companyId, 0, UserListType.COMPANY_USER);
            }
        });
    }


    private void initData(){
        startLoadData(true);
    }

    /**
     * 加载listview数据
     */
    private int mPageIndex = 1;
    private int mPageSize = 10;
    private long mLastReqTime = System.currentTimeMillis();
    protected void startLoadData(boolean cleanData) {
        if (cleanData) {
            mPageIndex = 1;
            mLastReqTime = 0;
            mList.clear();
        }
        mController.searchCompany(mSearchContent, mPageIndex, mPageSize);
    }

    @Override
    public void onSearch(SearchSourcePager sourcePager, SearchType searchType, String searchContent) {
        mSearchSourcePager = sourcePager;
        mSearchType = searchType;
        mSearchContent = searchContent;
    }
}
